package com.cynteka.cyfoman.core;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * User: Sergey
 * Date: 17.07.11
 * Time: 14:29
 */
public class AutoIncrementFunction implements Function<Integer> {
    private String name = "next_id";
    private DataSource dataSource;
    private String procedureName = "next_id";

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public String getProcedureName() {
        return procedureName;
    }

    public void setProcedureName(String procedureName) {
        this.procedureName = procedureName;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Integer getValue() {
        JdbcTemplate template = new JdbcTemplate(dataSource);
        String sql = String.format("select %1s ()", procedureName);
        int res = template.queryForInt(sql);
        return res;
    }
}
